package MTT::Form::Switches;

use HTML::FormHandler::Moose;
extends 'HTML::FormHandler';
#with 'HTML::FormHandler::Render::Simple';
with 'HTML::FormHandler::Render::Simple';
use namespace::autoclean;

use utf8;

has schema => ( is => 'ro', required => 1);

has_field types => ( type => 'Select', label => 'Тип' );
has_field id_type => ( type => 'Hidden' );
has_field type => ( type => 'Text', label => 'Название' );
has_field table_name => ( type => 'Text', label => 'Префикс таблицы' );
has_field type_enabled => ( type => 'Checkbox', label => 'Доступен', input_without_param => 0, checkbox_value => 1);

has_field switches => ( type => 'Select', label => 'Коммутатор' );
has_field id_switch => ( type => 'Hidden' );
has_field host_code => ( type => 'Text', label => 'Название' );
has_field switch_descr => ( type => 'Text', label => 'Описание' );
has_field switch_type => ( type => 'Select', label => 'Тип' );
has_field updated => ( type => 'Text', label => 'Дата обновления' );
has_field created => ( type => 'Text', label => 'Дата создания' );
has_field switch_enabled => ( 
	type => 'Checkbox', 
	label => 'Доступен', 
	input_without_param => 0, 
	checkbox_value => 1 
);

sub options_types {
	my ($self) = @_;
	return unless $self->schema;

	return map { { value => $_->id, label => $_->type } } $self->schema->resultset('SwitchType')->all();
}

sub options_switches {
	my ($self) = @_;
	return unless $self->schema;

	return map { { value => $_->id, label => $_->host_code . ' (' . $_->descr . ')' } } $self->schema->resultset('Switch')->all();
}

sub options_switch_type {
	my ($self) = @_;
	return unless $self->schema;

	return map { { value => $_->id, label => $_->type } } $self->schema->resultset('SwitchType')->all();
}

__PACKAGE__->meta->make_immutable;
1;
